package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.LecturerBiz;
import com.njym.java.security.app.biz.SupportBussinessBiz;
import com.njym.java.security.app.biz.TravelBussinessRelationBiz;
import com.njym.java.security.app.entity.Lecturer;
import com.njym.java.security.app.entity.SupportBussiness;
import com.njym.java.security.app.entity.TravelBussinessRelation;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
@RequestMapping("travelBussinessRelation")
public class TravelBussinessRelationController extends BaseController<TravelBussinessRelationBiz,TravelBussinessRelation> {

    @Autowired
    private SupportBussinessBiz supportBussinessBiz;
    @Autowired
    private LecturerBiz lecturerBiz;

    @RequestMapping(value = "/toBeTeacher", method = RequestMethod.GET)
    @ResponseBody
    public Object toBeTeacher(String name, String brief, String mobile, Integer province){
        try {
            String convert = getPinYinHeadChar(name);
            Lecturer entity = new Lecturer();
            entity.setName(name);
            entity.setBrief(brief);
            entity.setPhoto("");
            entity.setVideoUrl("");
            entity.setAttr3(mobile);
            entity.setAttr2(convert.toUpperCase());
            entity.setAttr1(province);
            entity.setAttr4(0);
            lecturerBiz.insertSelective(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }

    }


    @Transactional(rollbackFor = RuntimeException.class)
    @RequestMapping(value = "/addRelation", method = RequestMethod.GET)
    @ResponseBody
    public Object addRelation(String name, String brief, String address,
                              String contactMan, String mobile, String telephone, String province){
        Example example = new Example(SupportBussiness.class);
        example.createCriteria().andEqualTo("name",name);
        List<SupportBussiness> l = supportBussinessBiz.selectByExample(example);
        if (l.size() > 0) {
            return new ObjectRestResponse<>().rel(true).data("已经提交过同名企业的承办申请了！");
        }else {
//            try {
                SupportBussiness entity = new SupportBussiness();
                entity.setName(name);
                entity.setBrief(brief);
                entity.setAddress(address);
                entity.setContactMan(contactMan);
                entity.setMobile(mobile);
                entity.setTelephone(telephone);
                entity.setAttr1(0);
                entity.setAttr2(province);
                entity.setAttr3(getPinYinHeadChar(name).toUpperCase().substring(0,1));
                supportBussinessBiz.insertBussiness(entity);        //插入企业表数据，返回一个主键id

                //在关联表插入数据
//                TravelBussinessRelation travelBussinessRelation = new TravelBussinessRelation();
//                travelBussinessRelation.setBussinessId(entity.getId());
//                travelBussinessRelation.setModiffyTime(new Date());
//            travelBussinessRelation.setTravelId(travelId);
//                travelBussinessRelation.setStatus(0);
//                baseBiz.insertSelective(travelBussinessRelation);
                return new ObjectRestResponse<>().rel(true);
//            }catch (RuntimeException e){
//                return new ObjectRestResponse<>().rel(false);
//            }
        }
    }

    /**
     * 中文转首字符
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }
}